package com.ftg.learn.chapter08.pattern.factory.pets;

import java.util.Random;

/**
 * 宠物工厂
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/3 20:29
 */

public class PetFactory {

    /**
     * 随机一个宠物
     * @return
     */
    public static Pet getPet(){
        Random r = new Random();
        int a = r.nextInt(3);
        switch (a){
            case 0:
                return new DogPet();
            case 1:
                return new CatPet();
            default:
                return new MousePet();
        }
    }

    public static Pet getPet(String type){
        switch (type){
            case "dog":
                return new DogPet();
            case "cat":
                return new CatPet();
            case "mouse":
                return new MousePet();
            default:
                return null;
        }
    }

    public static void main(String[] args) {

        int a = 23;

        long l = a;

        a = (int)l;

        // 向上造型
        Pet p2 = new CatPet();
        // 向下造型 (强制类型转换)
        CatPet p3 = (CatPet) p2;

//        PetFactory.getPet("cat");  new CatPet(); 就是一个猫

        // Pet p1 =  引用指向了 猫

        Pet p1 = PetFactory.getPet("cat");
        p1.info();

        CatPet cat1 = (CatPet) p1;
        cat1.info();
        cat1.nao();

        PetFactory.getPet("cat").info();
        PetFactory.getPet("cat").info();
        PetFactory.getPet("cat").info();
    }
}
